/**
 * Created by Administrator on 2016/10/5.
 */
var user = (function($){
    return{

        tableObj : null,
        otherElem : {},
        // 初始化
        init: function(){
            type = momo.getURLElement('type'); // 全局变量
            momo.initPage();// 左边菜单栏
            momo.download.call(this, "PEnterTask/export");// 导出
            this.addBtnEvent();// 给“搜索”“重置”按钮添加点击事件

            //动态接受参数进行设置
            var isIFrame = momo.getURLElement("isIFrame");// 是否是模态框
            //console.log( isIFrame );



                // iframe内部页面 移除 头部、左边菜单栏

                // 根据搜索条件初始化表格数据
                var name = $('#searchName').val();
                var phone = $('#searchPhone').val();
                var account = $('#searchAccount').val();

                var _data = {
                    "name": name,
                    "phone": phone,
                    "account": account,
                    "task_id":momo.getURLElement('id')
                };
                this.initTable( _data, false );// 初始化表格



            // 导出
            this.otherElem = { };
        },

        // 初始化表格
        initTable: function( data, isIFrame ){
            console.log(data);
            var _self = this;

            var url = 'PEnterTask/userlist';
            var editUrl = '';
            var colName = ['序号','姓名','手机号码','银行卡号','报名时间','完成任务时间','获得积分奖励'];//,'操作'];
            var colModal = [
                { index: 'id', width:5, textOverflowEllipsis: true, sort: false, formatter: setNum},
                { index: 'name', width:10, textOverflowEllipsis: true, sort: false },
                { index: 'phone', width:15, textOverflowEllipsis: true, sort: false },
                { index: 'account', width:20, textOverflowEllipsis: true, sort: false },
                { index: 'add_time', width:10, textOverflowEllipsis: true, sort: false },
                { index: 'finish_time', width:10, textOverflowEllipsis: true, sort: false },
                { index: 'scope', width:20, textOverflowEllipsis: true, sort: false },
            ];

            // 如果是iframe，不显示“操作”
            if( isIFrame ){
                colName.pop();
                colModal.pop();
            }

            var _data = {};
            if( data != undefined ) _data = $.extend( _data, data );
            this.tableObj = new $.jqGrid( $('#jqGrid'), url, _data, colName, colModal, undefined, false, true, editUrl );
             console.log(this.tableObj);


            // （辅助函数）设置序号
            function setNum( val, rowData, index ) {
                var _num = ( _self.tableObj.currentPage - 1 ) *  _self.tableObj.data.page_size + ( index + 1 );
                return '<span>' + parseInt( _num ) + '</span>';
            }

        },

     
        // 给“搜索”“重置”按钮、“展开/收起日志”、“取消订单”按钮、订单备注编辑、保存、取消按钮 添加点击事件
        addBtnEvent: function(){

            var _self = this;

            // 点击搜索按钮
            $('.searchBtn').click( function(){

                var name = $('#searchName').val();
                var phone = $('#searchPhone').val();
                var account = $('#searchAccount').val();

                var _data = {
                    "name": name,
                    "phone": phone,
                    "account": account,
                    "task_id":momo.getURLElement('id')
                };
                this.tableObj.reloadData( _data, true );

                this.otherElem = $.extend( this.otherElem, _data);// 导出功能

            }.bind( this ));

            // 点击重置按钮
            $('.resetBtn ').click( function(){
                $("#searchName").val('');
                $("#searchPhone").val('');
                $("#searchAccount").val('');

            });
        }
    }
})(jQuery);

var init = user.init.bind( user );
